package com.xs.ai.config;



import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.reader.TextReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

// 配置类标识
@Configuration
public class ChatConfig {

    // 定义ChatMemory类型的Bean，使用内存实现
    @Bean
    public ChatMemory chatMemory() {
        return new InMemoryChatMemory();
    }

    // 可以在这里添加其他Bean定义（如VectorStore、ChatClient等）

@Bean
public VectorStore vectorStore(EmbeddingModel embeddingModel) {
    return new SimpleVectorStore(embeddingModel);
}

    @Bean
    CommandLineRunner ingestTermOfServiceToVectorStore(EmbeddingModel embeddingModel, VectorStore vectorStore,
                                                       @Value("classpath:rag/terms-of-service.txt") Resource termsOfServiceDocs){
return args -> {
    vectorStore.write(new TokenTextSplitter().transform(new TextReader(termsOfServiceDocs).read()));

};

}

}
